﻿using PF;
using UnityEngine;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;
using ETModel;

namespace ETHotfix {
    [ObjectSystem]
    public class GameUnitAwakeSystem : AwakeSystem<GameUnit, GameObject, UnitInfo> {
        public override void Awake(GameUnit self, GameObject gameObject, UnitInfo unitInfo) {
            self.Awake(gameObject, unitInfo);
        }
    }

    [HideInHierarchy]
    public class GameUnit : Unit {

        private Vector3 _Position;
        private Vector3 _CenterPosition;
        private Quaternion _Rotation;
        private Vector3 _Scale;
        private Vector3 _Center;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameObject">传进来,GameUnit里负责销毁</param>
        /// <param name="unitInfo"></param>
		public void Awake(GameObject gameObject, UnitInfo unitInfo) {
            Awake(gameObject);

            _Center = new Vector3(unitInfo.CenterX, unitInfo.CenterY, unitInfo.CenterZ);
            _CenterPosition = unitInfo.GetCenterPosition();
            Position = _CenterPosition - _Center;
            Rotation = unitInfo.GetRotation();
            Scale = unitInfo.GetScale();
        }
        public override void Dispose() {
            if (this.IsDisposed) {
                return;
            }

            base.Dispose();
        }

        private void UpdateUnitWorldTransform() {
            GizmosDebug.Instance.UpdateUnitPos(Id, _CenterPosition, _Rotation, _Scale);
        }

        public override Vector3 Position {
            get {
                return base.Position;
            }
            set {
                base.Position = value;
                _Position = value;
                _CenterPosition = _Position + _Center;
                UpdateUnitWorldTransform();
            }
        }
        public void SetCenterPosition(Vector3 centerPos) {
            _CenterPosition = centerPos;
            Position = _CenterPosition - _Center;
        }

        public override Quaternion Rotation {
            get {
                return base.Rotation;
            }
            set {
                _Rotation = value;
                base.Rotation = value;
                UpdateUnitWorldTransform();
            }
        }
        public override Vector3 Scale {
            get {
                return base.Scale;
            }
            set {
                _Scale = value;
                base.Scale = value;
                UpdateUnitWorldTransform();
            }
        }

    }
}